package net.dntuan.training.fop;

import java.io.File;

import org.apache.avalon.framework.ExceptionUtil;
import org.apache.fop.apps.Options;

public class DigitasXML2PDF {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			System.out.println("FOP XML2PDF\n");
			System.out.println("Preparing...");

			// Setup directories
			File baseDir = new File(".", "src/main/resources/digitas");

			File cnfFile = new File(baseDir, "userconfig.xml");
			if (cnfFile.exists()) {
                // magic codes
                new Options(cnfFile);
			} else {
				System.out.println("can't load the userconfig.xml file");
			}

			// Setup input and output files
			String inputData = "MURANO.xml";
			if (args != null && args.length > 0) {
				inputData = args[0];
			}
			File xmlfile = new File(baseDir, inputData);
			File xsltfile = new File(baseDir, "lcb.xsl");
			File pdffile = new File(".", xmlfile.getName().substring(0, xmlfile.getName().lastIndexOf('.')) + ".pdf");

			System.out.println("Input: XML (" + xmlfile + ")");
			System.out.println("Stylesheet: " + xsltfile);
			System.out.println("Output: PDF (" + pdffile + ")");
			System.out.println();
			System.out.println("Transforming...");

			ExampleXML2PDF app = new ExampleXML2PDF();
			app.convertXML2PDF(xmlfile, xsltfile, pdffile);

			System.out.println("Success!");
		} catch (Exception e) {
			System.err.println(ExceptionUtil.printStackTrace(e));
			System.exit(-1);
		}
	}

}
